package com.cheyun.netsalev3.viewmodel

import android.content.Intent
import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import androidx.paging.PagedList
import com.cheyun.netsalev3.bean.dm.DmClueList
import com.cheyun.netsalev3.repository.DmPageRepository
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.view.dm.detail.DmDetailsActivity

class DmXktViewModel(var repository: HashMap<Int,DmPageRepository>) : BaseViewModel() {
    fun onClickItem(view: View, item: DmClueList) {
        var intent = Intent(view.context, DmDetailsActivity::class.java)
        intent.putExtra("id",item.id.toString())
        view.context.startActivity(intent)
    }

    //选中的列表数据
    var pagedlist = HashMap<Int, LiveData<PagedList<DmClueList>>>()
    var hashMap = HashMap<Int, LiveData<NetworkState>>()
    var totlelist = HashMap<Int, LiveData<Int>>()

    init {
        for (key in repository.keys) {
            repository[key]?.apply {
                pagedlist[key] = this.loadAndroidData()
            }
        }
    }

    init {
        for (item in repository.keys) {
            pagedlist[item]?.apply {
                hashMap[item] = Transformations.switchMap(this) { repository[item]!!.networkState }!!
                totlelist[item] = Transformations.switchMap(this) { repository[item]!!.total }!!
            }
        }
    }

    /**
     * 刷新
     */
    fun refresh(nkey: Int) {
        repository[nkey]?.dataSourceFactory?.sourceLiveData?.value?.invalidate()
    }
}
